﻿using Microsoft.AspNetCore.Mvc;
using miniMana.Data.MyDataBaseContext;
using miniMana.Entity;
using miniMana.IService;
using miniMana.Request;
using miniMana.Service;

namespace miniMana.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {

        //public  MyDataBaseContext_main context;
        //public UserController(MyDataBaseContext_main myDataBaseContext_Main)
        //{
        //    context = myDataBaseContext_Main;
        //}

        ///// <summary>
        ///// 添加用户
        ///// </summary>
        ///// <returns></returns>
        //[HttpPost("[action]")]
        //public bool AddUser(AddUserRequest request)
        //{
        //    var user = new User()
        //    {
        //        UserAddress = request.UserAddress,
        //        UserAge = request.UserAge,
        //        UserName = request.UserName,
        //        UserPwd = request.UserPwd,
        //    };
        //    context.Users.Add(user);
        //    context.SaveChanges();
        //    return true;
        //}

        public IUserService _userService { get; set; }
        public UserController(IUserService userService)
        {
            _userService = userService;
        }

        [HttpPost]
        public async Task<bool> TryLogin(string account, string password)
        {
            //查询账号信息
            var accountEty = await _userService.GetAccountMsg(account);
            if (accountEty != null)
            {
                if (password == accountEty.Pwd)
                {
                    //更新账号最后一次登录时间
                    await _userService.UpdateLoginTime(accountEty);

                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
}
